﻿using FileStorage.Models.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace FileStorage.Common
{
    public static class TextFormatUtil
    {
        public static string getNewNodeName(string originalName, List<Node> nodes)
        {
            int i = 0;
            List<string> fileNames = nodes.Select(x => x.FileName).ToList();
            string newName = originalName;
            while (fileNames.Contains(newName))
            {
                i++;
                if (originalName.IndexOf(".") >= 0)
                {
                    newName = originalName.Substring(0, originalName.LastIndexOf(".")) + " (" + i + ")" + originalName.Substring(originalName.LastIndexOf("."));
                    continue;
                }
                newName = originalName + " (" + i + ")";
            }
            return newName;
        }

        public static bool matcherFolderName(string folderName)
        {
            return !Regex.IsMatch(folderName, "[|\\/*<>\"?&$:]+");
        }

        public static bool matcherFileName(string fileName)
        {
            return !Regex.IsMatch(fileName, "[|\\/*<>\"?&$:]+");
        }

        public static bool hasEscapes(string input)
        {
            return input.IndexOf("\\") >= 0;
        }
    }
}
